package com.xltq.controller;

import com.xltq.entity.Cart;
import com.xltq.entity.User;
import com.xltq.service.ICartService;
import com.xltq.service.impl.ICartServiceImpl;
import com.xltq.utils.Constants;
import org.omg.PortableInterceptor.INACTIVE;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

@WebServlet("/cart")
public class CartController extends BaseServlet {
    //1.构建购物车
    public String create(HttpServletRequest request,HttpServletResponse response) throws Exception{
        //1.判断首页中购物车是否可以点击进入到购物车页面
        //只要判断session中存储的登录信息对象即可
        User loginUser = (User)request.getSession().getAttribute("loginUser");
        if(Objects.isNull(loginUser)){
            //相当于没有登录用户信息,所以不能访问购物车页面提示需要登录
            request.getSession().setAttribute("msg", "访问购物车必须先登录!");
            return Constants.REDIRECT+"/login.jsp";
        }
        //2.已经登录
        int uid = loginUser.getUid();
        //接收前端传递过来的商品id
        String pid = request.getParameter("pid");
        ICartService cartService = new ICartServiceImpl();
        cartService.crateCart(uid, Integer.valueOf(pid));

        return Constants.FORWARD+"/cartSuccess.jsp";
    }
    //2.现实购物车信息
    public String show(HttpServletRequest request,HttpServletResponse response) throws Exception{
        //1.判断首页中购物车是否可以点击进入到购物车页面
        //只要判断session中存储的登录信息对象即可
        User loginUser = (User)request.getSession().getAttribute("loginUser");
        if(Objects.isNull(loginUser)){
            //相当于没有登录用户信息,所以不能访问购物车页面提示需要登录
            request.getSession().setAttribute("msg", "访问购物车必须先登录!");
            return Constants.REDIRECT+"/login.jsp";
        }
        //2.已经登录
        int uid = loginUser.getUid();
        ICartService cartService = new ICartServiceImpl();
        List<Cart> list = cartService.findAll(uid);
        //将得到信息存早作用域中,让前端进行展示
        request.setAttribute("list",list);
        return Constants.FORWARD+"/cart.jsp";
    }
    //3.购物车展示页面删除操作
    public String delete(HttpServletRequest request,HttpServletResponse response) throws Exception{
        //1.获取cid
        String cid = request.getParameter("cid");
        //2.调用业务层处理删除操作
        ICartService cartService = new ICartServiceImpl();
        cartService.deleteCartByCid(Integer.valueOf(cid));
        //3.转发到展示页面进行数据展示操作
        //不要直接跳转页面(页面没有数据),只有跳转servlet方法触发再次查询的到数据并进行展示
        return  Constants.FORWARD+"cart?method=show";
    }

    //4.购物车车展示页面清除操作
    public String clear(HttpServletRequest request,HttpServletResponse response) throws Exception{
        //1.获取uid
        String uid = request.getParameter("uid");
        //2.调用业务层处理删除操作
        ICartService cartService = new ICartServiceImpl();
        cartService.clearCart(uid);
        //3.转发到展示页面进行数据展示操作
        //不要直接跳转页面(页面没有数据),只有跳转servlet方法触发再次查询的到数据并进行展示
        return  Constants.FORWARD+"cart?method=show";
    }

    //5.购物车页面展示更新操作[数量和小计]
    public String update(HttpServletRequest request,HttpServletResponse response) throws Exception{
        //1.获取uid  price(价格) cnum(数量)
        String cid = request.getParameter("cid");
        String price = request.getParameter("price");
        String cnum = request.getParameter("cnum");
        //2.调用service层处理操作
        ICartService cartService = new ICartServiceImpl();
        cartService.updateCartByCid(cid, price, cnum);
        //3.转发到展示页面进行数据展示操作
        //不要直接跳转页面(页面没有数据),只有跳转servlet方法触发再次查询的到数据并进行展示
        return  Constants.FORWARD+"cart?method=show";
    }
}
